import os
import random
import string
import requests

def p(val, type=1):
    print(val)
    if type == 1:
        exit()


def has_nested_dict(d):
    for value in d.values():
        if isinstance(value, dict):
            return True
    return False


# 读取文本内容
def getFileContent(fileName):
    if not os.path.exists(fileName):
        return False
    with open(fileName, 'r', encoding='utf-8') as file:
        content = file.read()
        return content


def putFileContent(cont, fileName='jobs.txt'):
    if not os.path.exists(fileName):
        with open(fileName, 'w') as file:
            file.write('')
    with open(fileName, 'a', encoding='utf-8') as file:
        file.write(cont)

# 生成随机账号 1强密码 数字+字母+字符串 2生成字母+数字随机 3数字随机 4.
def generate_rand(length, char_type=1):
    char_sets = {
        1: string.ascii_letters + string.digits + string.punctuation,
        2: string.ascii_letters + string.digits,
        3: string.digits,
        4: string.digits
    }
    # 获取对应的字符集
    characters = char_sets.get(char_type, char_sets[4])
    # 生成随机字符串
    rtchar = ''.join(random.choice(characters) for _ in range(length))
    if char_type == 4:
        rtchar = '1' + rtchar
    return rtchar

#注册的昵称
def generate_chinese_nickname(count=1):
    # 定义昵称的前缀、中缀和后缀列表
    first = ["优雅", "从容", "淡定", "忧伤", "犹豫", "清纯", "秀丽"]
    second = ["琴声", "居住", "沧桑", "厚重", "流水", "无痕", "轮回"]
    last = ["少年", "少女", "老人", "兄弟", "妈妈", "爸爸", "爷爷", "奶奶"]
    # 使用列表推导式生成昵称列表
    nicknames = [
        random.choice(first) + random.choice(second) + random.choice(last)
        for _ in range(count)
    ]
    return nicknames

#下载图片常用
def download_image(url, save_path):
    headers = {
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1',
    }
    response = requests.get(url,headers=headers)
    if response.status_code == 200:
        with open(save_path, 'wb') as file:
            file.write(response.content)
        print(f"图片已保存到 {save_path}")
    else:
        print(f"请求失败，状态码：{response.status_code}")